<?php
/**
 * Created by Sublime.
 * @Author lingyun 
 * @Time:  2017-11-24 16:53:45
 */
namespace app\mall\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use think\Request;
class Order extends BasicAdmin{
	/**
	 * 订单列表
	 * @Author   lingyun
	 * @DateTime 2017-11-24T16:54:10+0800
	 * @return   [type]                   [description]
	 */
	public function index(){
		$this->title = "订单列表";
		$table = "Order";
		$get = $this->request->get();
		$join = [
			['ys_user b','b.uid=a.uid','LEFT'],
			['ys_order_details c','c.order_id=a.id']
		];
		isset($get['type'])?$get['type']=$get['type']:$get['type']=1;		 
		if(isset($get['type']) && $get['type'] == 1){	//线上订单
			$map = ['a.order_type'=>['eq',1]];
		}else if(isset($get['type']) && $get['type'] == 2){	//线下订单
			$map = ['a.order_type'=>['eq',0]];
		}else{
		    $map = array();
        }
		$db = Db::name('order')->alias('a')->join($join)->field('a.id,a.order_sn,a.pay_type,a.pay_status,a.goods_num,a.final_amount,a.addtime,a.order_type,a.order_status,b.realname,b.mobile,count(c.tuihuo_time) as is_tuihuo,(select count(*) from ys_order_details where order_id=a.id) as count')->where($map)->order('a.id desc')->group('c.order_id');
        (isset($get['order_sn']) && $get['order_sn'] !== '') && $db->whereLike('a.order_sn', "%{$get['order_sn']}%");		//订单号查询
        (isset($get['store_id']) && $get['store_id'] !== '') && $db->where('a.store_id',$get['store_id']);		//订单号查询
        if(isset($get['date']) && $get['date'] !== '') {
            // list($start, $end) = explode('-', str_replace(' ', '', $get['date']));
            list($start, $end) = explode(' - ', $get['date']);
            $start = $start."  00:00:00";		$end = $end." 23:59:59";
            $start = strtotime($start);			$end = strtotime($end);
            $db->whereBetween('a.addtime',["{$start}", "{$end}"]);
        }
        $store = Db::name('store')->where(['status'=>1,'is_deleted'=>0])->field('id,title,addtime')->select();
        $this->assign('store',$store);
        return parent::_list($db);
	}
	/**
	 * 订单详情
	 * @Author   lingyun
	 * @DateTime 2017-11-24T17:05:04+0800
	 * @return   [type]                   [description]
	 */
	public function detail(){
		$id = input('id');		//订单id主键ID
		$join = [
			['ys_goods b','a.goods_id=b.id','LEFT'],
			['ys_order c','a.order_id=c.id','LEFT'],
		];
		//下单人信息以及订单信息
		$info = Db::name('user')->alias('d')->join('ys_order c','d.uid=c.uid','LEFT')->field('d.realname,d.mobile,d.uid,d.email,c.order_sn,c.consignee,c.address,c.zipcode,c.id,c.final_amount,c.order_status,c.addtime,c.pay_time,c.pay_type,c.score,c.goods_price,c.dis_type,c.discount,c.user_note,c.admin_note,c.coupon_price,c.order_type,shipping_price')->where('c.id',$id)->find();
		//订单操作记录
		$action = Db::name('order_action')->alias('f')->join('ys_system_user e','f.action_user=e.id','LEFT')->field('f.*,e.username')->where('f.order_id',$id)->order('f.id asc')->select();		//订单action
//		 halt($action);
		//订单商品详情
		$db = Db::name('order_details')->alias('a')->join($join)->where('a.order_id',$id)->field('a.id,a.spec_detail,a.goods_num,a.status,a.tuihuo_sign,a.tuihuo_num,a.th_deal_remark,b.addtime,b.name,c.order_sn,c.order_type,c.pay_status,c.pay_type,c.final_amount,c.pay_time,c.consignee,c.country,c.province,c.city,c.district,c.twon,c.address,c.mobile,c.addtime as xiadan_time')->order('a.id desc');
		$this->assign('info',$info);
		$this->assign('action',$action);
        return parent::_list($db);
	}

	/**
	 * 退货原因
	 * @Author   lingyun
	 * @DateTime 2017-11-24T17:06:27+0800
	 * @return   [type]                   [description]
	 */
	public function back(){
		$id = input('id');
		$info = Db::name('order_details')->alias('a')->join('goods b','a.goods_id=b.id','LEFT')->join('ys_order c','a.order_id=c.id','LEFT')->where('a.id',$id)->field('a.id,a.spec_detail,a.tuihuo_time,a.reason,a.img,a.tuihuo_sign,a.tuihuo_num,a.tuihuo_amount,a.th_deal_result,a.th_deal_remark,a.th_deal_time,b.name,c.order_sn,c.pay_type,c.order_type')->find();
		$this->assign('info',$info);
		$this->assign('title','退货原因');
		return $this->fetch();
	}



}
?>